package com.weiiew.buildservice.controller;

import com.example.feignapi.client.RecipeClient;
import com.weiiew.buildservice.service.BuildingServiceImpl;
import com.weiiew.common.entity.dto.Building;
import com.weiiew.common.entity.qo.BuildingQo;
import com.weiiew.common.utils.ApiResponse;
import com.weiiew.common.GatawayAccess.GatewayAccess;
import com.weiiew.common.exception.messageException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 建筑物 controller 层
 * @Author 魏锴
 * @Date 2024/2/5
 */
@Slf4j
@RestController
@RequestMapping("/build")
@RequiredArgsConstructor
public class BuildingController {
    private final BuildingServiceImpl buildingService;
    private final RecipeClient recipeClient;

    /**
     * 插入建筑物信息
     *
     * @param building 建筑物信息
     * @return 插入或修改结果
     */
    @PostMapping("/insert")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse insertBuilding(
            @RequestHeader("my-version") Integer version,
            @RequestBody Building building) {
        boolean flag = buildingService.insertBuilding(building, building.getId(), version);
        if (flag) return ApiResponse.ok("建筑信息保存成功");
        else return ApiResponse.failure("建筑信息保存失败");
    }

    /**
     * 修改建筑物信息
     *
     * @param building 建筑物信息
     * @return 插入或修改结果
     */
    @PostMapping("/update")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse saveBuilding(
            @RequestHeader("my-version") Integer version,
            @RequestBody Building building) {
        boolean flag = buildingService.updateBuilding(building, building.getId(), version);
        if (flag) return ApiResponse.ok("建筑信息更新成功");
        else return ApiResponse.failure("建筑信息更新失败");
    }

    /**
     * 删除建筑物信息
     *
     * @param id 建筑物id
     * @return 删除结果
     */
    @PostMapping("/remove/{id}")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse removeBuilding(
            @RequestHeader("my-version") Integer version,
            @PathVariable Integer id) throws messageException {
        boolean flag = buildingService.removeBuilding(id, version);
        return ApiResponse.response(flag, "建筑删除");
    }

    /**
     * 获取建筑修改栏初始化数据, 根据参数获取建筑列表
     *
     * @param version    版本号
     * @return 建筑信息
     */
    @GetMapping("/init")
    @GatewayAccess({GatewayAccess.GatewayType.MANAGER, GatewayAccess.GatewayType.CLIENT})
    public ApiResponse init(@RequestHeader("my-version") Integer version) {
        if (version == null) return ApiResponse.failure("获取失败");
        return ApiResponse.ok("初始化参数获取成功")
                .append("types", buildingService.getBuildingTypes(version))
                .append("costs", buildingService.getBuildingCosts(version));
    }

    /**
     * 返回建筑详细信息以及相关配方
     *
     * @param id 物品id
     * @return 物品详细信息以及相关配方
     */
    @GetMapping("/detail/{id}")
    @GatewayAccess({GatewayAccess.GatewayType.MANAGER, GatewayAccess.GatewayType.CLIENT})
    public ApiResponse getDetailBuild(@PathVariable Integer id) {
        return ApiResponse.ok("获取成功")
                .append("build", buildingService.getDetailBuild(id))
                .append("recipe", recipeClient.getRecipeByBuildingId(id));
    }

    /**
     * 根据搜索条件进行建筑搜索
     *
     * @param qo 查询条件
     * @return 满足条件的分页建筑列表和总长度
     */
    @PostMapping("/manager/list")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse getList(BuildingQo qo, @RequestHeader("my-version") Integer version){
        qo.format();
        qo.setVersion(version);
//        System.out.println("qo = " + qo);
        if (!qo.check()) return ApiResponse.failure("参数缺失");
        return ApiResponse.ok("获取成功")
                .append("list", buildingService.getBuildingList(qo))
                .append("length", buildingService.getBuildingLength(qo));
    }

    /**
     * 根据搜索条件进行建筑搜索
     *
     * @param qo 查询条件
     * @return 满足条件的分页建筑列表和总长度
     */
    @PostMapping("/client/list")
    @GatewayAccess(GatewayAccess.GatewayType.CLIENT)
    public ApiResponse getList(@RequestHeader("my-version") Integer version, BuildingQo qo){
        qo.format();
        qo.setVersion(version);
        System.out.println("qo = " + qo);
        if (version == null) return ApiResponse.failure("参数缺失");
        return ApiResponse.ok("获取成功").append("list", buildingService.getSimpleBuildingList(qo));
    }
    /**
     * 获取建筑详细信息包括消耗信息
     *
     * @param id 建筑id
     * @return 建筑信息
     */
    @GetMapping("/info/{id}")
    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    public ApiResponse getBuilding(@PathVariable Integer id) {
        Building buildingVO = buildingService.getBuildingById(id);
        if (buildingVO != null) return ApiResponse.ok("获取成功", "building", buildingVO);
        else return ApiResponse.failure("获取失败");
    }

    @GetMapping("/icon")
    @GatewayAccess(GatewayAccess.GatewayType.CLIENT)
    public ApiResponse getBuildIconList(@RequestHeader("my-version") Integer version){
        return ApiResponse.ok("获取成功").append("list", buildingService.getBuildIconList(version));
    }
}
